import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "stock.settings")
django.setup()
from apscheduler.schedulers.blocking import BlockingScheduler
from stockapp.views import updateAllStock, stockRecordCsvToDb
from forecast.lstm_mutiple import *
import pandas as pd


def fetch_stock_data():
    # 定时获取数据保存
    print("job_fetch_stock_data.........")
    updateAllStock()
    stockRecordCsvToDb()


def train_model():
    df = pd.read_csv('./data/allStock.csv')
    start_time = time.time()
    for ts_code in df['ts_code']:
        fun(ts_code)
    print("总耗时：")
    print(time.time() - start_time)


def APschedulerMonitor():
    # 创建调度器：BlockingScheduler
    scheduler = BlockingScheduler()

    # Runs from Monday to Friday at 18:30
    scheduler.add_job(fetch_stock_data, 'cron', day_of_week='mon-fri', hour=18, minute=00)
    scheduler.add_job(train_model, 'cron', day_of_week='mon-fri', hour=18, minute=30)
    scheduler.start()


if __name__ == "__main__":
    APschedulerMonitor()
